<?php
class Backend_Compiller_Controller extends Backend_Controller
{
	/**
	 * @var Config_File_Array
	 */
    protected $_packagesConfig;
    
    public function __construct(){
        parent::__construct();
        $this->_autoloaderConfig = $this->_configMain->get('autoloader');
        $this->_packagesConfig = Config::factory(Config::File_Array, $this->_autoloaderConfig['packagesConfig']);
    }
    
    
    public function indexAction(){
    	 $res = Resource::getInstance();   
    	 $res->addJs('/js/lib/extjs4/ux/RowExpander.js'  , 1); 
	     $res->addJs('/js/app/system/crud/compiller.js'  , 2);
	     $this->_resource->addInlineJs('
            var canPublish =  '.((integer)$this->_user->canPublish($this->_module)).';
        	var canEdit = '.((integer)$this->_user->canEdit($this->_module)).';
        	var canDelete = '.((integer)$this->_user->canDelete($this->_module)).';
        ');
    }
    
    public function listAction(){
    	$result = array();
    	$list = $this->_packagesConfig->get('packages');
    	foreach ($list as $name =>$data){
    		$result[] = array(
    			'id'=> $name,
    			'name'=> $name,
    			'active' => $data['active'],
    			'files_count'=>sizeof($data['paths']),
    			'files_list' => '<div class="fileListItem">'.implode('</div><div class="fileListItem">', $data['paths']).'</div>'		
    		);
    	}
    	
    	Response::jsonSuccess($result);
    }
    
    
    
    /**
     * @todo Доработать для множества языков
     * Compile JS Lang file (used current lang)
     */
    public function langAction()
    {
    	$langDir = $this->_configMain->get('docroot').'/system/lang/';
    	
    	$eng = Config::factory(Config::File_Array,($langDir.'eng.php'));
    	Lang::setDictionary($eng);
    	file_put_contents($this->_configMain->get('docroot').'/js/lang/eng.js', 'var appLang = '.Lang::getInstance()->getJsObject().';');

    	$ru = Config::factory(Config::File_Array,($langDir.'ru.php'));
    	Lang::setDictionary($ru);
    	file_put_contents($this->_configMain->get('docroot').'/js/lang/ru.js', 'var appLang = '.Lang::getInstance()->getJsObject().';');
    }
    
    public function packagesAction()
    {

    	$dest = $this->_packagesConfig->get('path');
    	
    	if($this->_packagesConfig->get('all_in_one'))
    	{
    		$s='';
    		$packages = $this->_packagesConfig->get('packages');
    		
    		foreach ($packages as $item){
    			if(!$item['active'])
    				continue;
    			$s.= $this->_compilePackage($item);
    		} 
    		Utils::exportCode($dest . $this->_packagesConfig->get('main_package').'.php', $s); 		
    	}
    	else
    	{
    		$packages = $this->_packagesConfig->get('packages');		
    		foreach ($packages as $name => $item)
    		{
    			if(!$item['active'])
    				continue;
    			Utils::exportCode($dest . $name .'.php', $this->_compilePackage($item));
    		}
    	}
    	
    	Utils::createClassMap(array(
    		'./system/app/',
    		'./system/library/'
    	),  $this->_autoloaderConfig['map'] , $this->_packagesConfig);
    }
    
    protected function _compilePackage($item)
    {
    	$s ='';
    	
    	foreach ($item['paths'] as $path)
    		if(is_file($path)) 		
    			$s.=str_replace(array('<?php' , '?>'), ' ',php_strip_whitespace($path));
    		
    	return $s;	
    }
}